package com.meixiang.activity.moments;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.adapter.account.MyPostDetailAdapter;
import com.meixiang.entity.moments.PostDetailsList;
import com.meixiang.entity.moments.PostDetailsItemBean;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/7 11:31
 *
 * 我的帖子详情
 */
public class MyPostDetailActivity extends BaseActivity implements OnRefreshListener {
    @Bind(R.id.tv_comment_number)
    TextView mTvCommentNumber;
    @Bind(R.id.tv_back)
    ImageView mTvBack;
    @Bind(R.id.tv_title)
    TextView mTvTitle;
    @Bind(R.id.iv_comment)
    ImageView mIvComment;
    @Bind(R.id.iv_praise)
    ImageView mIvPraise;
    @Bind(R.id.iv_share)
    ImageView mIvShare;
    @Bind(R.id.ll_input_dialog_box)
    LinearLayout mLlInputDialogBox;
    @Bind(R.id.tv_praise_number)
    TextView mTvPraiseNumber;
    @Bind(R.id.et_input_dialog_box)
    ClearEditText mEdInputDialogBox;
    @Bind(R.id.tv_send_message)
    TextView mTvSendMessage;
    @Bind(R.id.refresh)
    SwipeToLoadLayout mRefresh;

    private int pageNo = 1, pageSize = 2, totalPage = 0;


    private Activity mActivity;
    @Bind(R.id.swipe_target)
    RecyclerView mRecyclerView;

//    private PostDetailsAdapter mAdapter;

    private MyPostDetailAdapter mAdapter;
    private PostDetailsList mBean;

    private List<PostDetailsItemBean> mList;
    private boolean isLikeClick = false;


    private boolean isRefresh = true;

    private String CommentId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_post_details);
        ButterKnife.bind(this);
        mActivity=MyPostDetailActivity.this;
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        mTvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mIvShare.setOnClickListener(this);
    }

    @Override
    protected void initView() {
        mRefresh.setLoadingMore(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new MyPostDetailAdapter(mActivity, mActivity);
        mRecyclerView.setAdapter(mAdapter);
        mTvSendMessage.setOnClickListener(this);
        mAdapter.setOnItemClickListener(new MyPostDetailAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                autoPopupKeyboard();
            }
        });


        mIvComment.setOnClickListener(this);
        mIvPraise.setOnClickListener(this);
        mLlInputDialogBox.setOnClickListener(this);
    }

    @Override
    protected void setData() {

        if(getIntent().getExtras().getString("commentId")!=null){
            CommentId=getIntent().getExtras().getString("commentId");
        }

        getDetailedData();
    }

    public void getDetailedData(){
        HttpParams params = new HttpParams();
        params.put("pageSize", pageSize + "");
        params.put("pageNo", pageNo + "");
        params.put("commentId",CommentId);
        HttpUtils.post(Config.MOMENTS_POST_DETAILS, params, new HttpCallBack(mActivity) {

            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                String json = null;
                try {
                    onCompleteRefresh();
                    PostDetailsList mData = new Gson().fromJson(result.toString(), PostDetailsList.class);
                    json = result.getString("commentList");
                    mList = new Gson().fromJson(json, new TypeToken<List<PostDetailsItemBean>>() {
                    }.getType());
                    mAdapter.addAll(mList);
                    mAdapter.addData(mData);
                    mTvCommentNumber.setText(mData.getCommentNumber());
                    mTvPraiseNumber.setText(mData.getLaudNumber());
                    AbToastUtil.showToast(context,message);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context,message);
            }
        });


    }



    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_share:
                break;
            case R.id.ll_input_dialog_box:
                autoPopupKeyboard();
                break;
            case R.id.iv_comment:
                autoPopupKeyboard();
                break;
            case R.id.et_input_dialog_box:
                autoPopupKeyboard();
                break;
            case R.id.tv_send_message:
                doSendMessage();
                break;
            case R.id.iv_praise:
                doLikeRraise();
                break;
        }
    }

    private void doLikeRraise() {
        if (isLikeClick==false) {
            HttpParams params = new HttpParams();
            params.put("commentId", mTvCommentNumber.getText().toString());
            HttpUtils.post(Config.MOMENTS_ADD_LAUD, params, new HttpCallBack(mActivity) {
                @Override
                public void onSucceed(JSONObject result, String code, String message) {
                    Tool.showTextToast(context,message);
                    mTvPraiseNumber.setText(result.optString("laudNumber"));
                    mTvPraiseNumber.setTextColor(ContextCompat.getColor(context, R.color.magenta));
                    mIvPraise.setImageResource(R.mipmap.praise_like);
                    Tool.Like(mActivity,mTvPraiseNumber);
                    isLikeClick=true;
                }

                @Override
                public void onError(String code, String message) {
                    Tool.showTextToast(context,message);
                }
            });

        }
        else{
            HttpParams params = new HttpParams();
            params.put("commentId", mTvCommentNumber.getText().toString());
            HttpUtils.post(Config.MOMENTS_CANCEL_LAUD, params, new HttpCallBack(mActivity) {
                @Override
                public void onSucceed(JSONObject result, String code, String message) {
                    Tool.showTextToast(context, message);
                    mTvCommentNumber.setText(result.optString("laudNumber"));
                    mIvPraise.setImageResource(R.mipmap.praise);
                    mTvCommentNumber.setTextColor(ContextCompat.getColor(context, R.color.black_9));
                    isLikeClick=false;
                    Tool.showTextToast(context,message);
                }

                @Override
                public void onError(String code, String message) {
                    Tool.showTextToast(context,message);
                }
            });
        }
    }

    private void doSendMessage() {
        /**
         *replyMemberId 评论id
         *commentId   回复id
         *comment   评论内容
         * */
        HttpParams params = new HttpParams();
        params.put("replyMemberId","");
        params.put("commentId",CommentId);
        params.put("comment",mEdInputDialogBox.getText().toString());
        HttpUtils.post(Config.MOMENTS_COMMENT_EDIT, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                /**取消获取焦点  清空数据*/
                mEdInputDialogBox.clearFocus();
                mEdInputDialogBox.setInputType(InputType.TYPE_NULL);
                mEdInputDialogBox.setText("");
                Tool.showTextToast(context,message);
                /**刷新列表数据*/
                onRefresh();
            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(context,message);

            }
        });



    }
    private void onCompleteRefresh() {
        if (mRefresh != null) {
            if (isRefresh == true) {
                mRefresh.setRefreshing(false);
        }
        }
    }
    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        // TODO 刷新
        pageNo = 1;
        isRefresh = false;
        getDetailedData();
    }


    /**
     *
     * 自动获取editext焦点
     * 弹出键盘
     *
     * */
    public void autoPopupKeyboard(){
        mEdInputDialogBox.requestFocus();//获得焦点
        InputMethodManager mInputMethodManager= (InputMethodManager) mEdInputDialogBox.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.toggleSoftInput(0,InputMethodManager.SHOW_FORCED);
    }

}
